package at.irian.jsfatwork.broker.application;

import at.irian.jsfatwork.broker.domain.DomainModelException;
import at.irian.jsfatwork.broker.domain.order.Order;
import at.irian.jsfatwork.broker.domain.order.OrderResponse;
import at.irian.jsfatwork.broker.domain.order.OrderStatus;
import at.irian.jsfatwork.broker.domain.stock.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author Thomas Spiegl
 */
@Service
public class StockService {

    @Autowired
    private QuoteService quoteService;


    public OrderResponse sendAndExecuteOrder(Order order) {
        return new OrderResponse(
                getQuote(order.getStock())
                    .getAmount().multiply(new BigDecimal(order.getQuantity())),
                OrderStatus.COMPLETE);
    }
    

    private Quote getQuote(Stock stock) {
        Quote quote = quoteService.getQuote(stock);
        if (quote == null) {
            throw new DomainModelException("No quote for stock (" + stock.getSymbol()  + ") found");
        }
        return quote;
    }

}
